#include "S_system.h"



int Sys_ticket_num(FlightSystem Flight_sys,int id ,int num)
{
    D_List_file_scanf(Flight_sys.Flight_list);  
    printf("test");
    D_list_up_data_ticket(Flight_sys.Flight_list,D_List_find_id(Flight_sys.Flight_list, id),num);
    printf("num：%d,id:%d",num ,id);
    D_List_print(Flight_sys.Flight_list);
    D_List_file_print(Flight_sys.Flight_list);
    return 0;
}


const wchar_t *title_ticket= L"购票菜单";
const wchar_t *option_ticket[] ={L"购票",L"退票",L"退出"};

//购票退票
int Sys_user_ticket(FlightSystem Flight_sys,U_data log_data,F_list *List)
{
    int num=0;     //买票的下标
    int num_id;
    int num_d=-1;   //退票的下标
    int flight_fare;
    int tick_flag=0;

    num=display_menu(title_ticket, option_ticket, 3);
    switch (num)
    {
    case 1:
        D_List_print(Flight_sys.Flight_list);
        printf("请输入购买哪一班次的航班:");
        scanf("%d",&num_id);
        for (size_t i = 0; i <10; i++)           
        if(log_data.ticket[i].id==num_id)
        {  
            printf("\n\n重复购买,退出系统!!");                          //重复购买，
            return 0;                                    
        }                
        printf("购买班次为%d的机票回车以确认",num_id);
        getchar();
        getchar();

        break;
    case 2:
        printf("请输入要退那一班次的航班:");
        scanf("%d",&num_id);

        for (size_t i = 0; i <10; i++)
        if(log_data.ticket[i].id==num_id)
        {
            num_d=i;
            tick_flag=1;
        }
        printf("退班次为%d的机票,持有的第%d张机票,回车以确认",num_id,num_d+1);
        getchar();
        getchar();
        break;
    default:
        return 0;
        break;
    }

    flight_fare=D_List_find_id(List,num_id)->data.value;
    if(1==num&&log_data.value<flight_fare)
    {
        printf("余额不足,请充值,回车以继续");
        getchar();
        return 0;
    }
    if(2==num&&0==tick_flag)
    {
        printf("无票可退,回车以继续");
        getchar();
        return 0;
    }

    if(log_data.grade==2)
    {    printf("inset1");
        if(1==num)
        {
        for (size_t i = 0; i <10; i++)
        
            if(log_data.ticket[i].id==0)
           {
            printf("inset2");
            if(1==num)
            S_list_add_id(Flight_sys.REGULAR_USER,S_List_find_name(Flight_sys.REGULAR_USER,log_data.name),num_id,i,flight_fare);
            Sys_ticket_num(Flight_sys,num_id ,-1);
            S_List_File_print(Flight_sys.REGULAR_USER,REGULAR_FILE); 
            return 0;  
           }
        
        }
        else if(2==num)
        {    printf("insetn2");
            S_list_add_id(Flight_sys.REGULAR_USER,S_List_find_name(Flight_sys.REGULAR_USER,log_data.name),0,num_d,-flight_fare);
            Sys_ticket_num(Flight_sys,num_id ,1);
            S_List_File_print(Flight_sys.REGULAR_USER,REGULAR_FILE); 
            return 0;
       }
    }
     if(log_data.grade==3)
    {    printf("inset1");
            if(1==num)
            {
            for (size_t i = 0; i <10; i++)
            if(log_data.ticket[i].id==0)
           {
          

            S_list_add_id(Flight_sys.SILVER_USER,S_List_find_name(Flight_sys.SILVER_USER,log_data.name),num_id,i,flight_fare*0.9);
            Sys_ticket_num(Flight_sys,num_id ,-1);
            S_List_File_print(Flight_sys.SILVER_USER,SILVER_FILE); 
            return 0;
           }
        }
            else if(2==num)
        {
            S_list_add_id(Flight_sys.SILVER_USER,S_List_find_name(Flight_sys.SILVER_USER,log_data.name),0,num_d,(-flight_fare)*0.9);
            Sys_ticket_num(Flight_sys,num_id ,1);
            S_List_File_print(Flight_sys.SILVER_USER,SILVER_FILE); 
            return 0;
        } 
    }
   
     if(log_data.grade==4)
    {    printf("inset1");
        if(1==num)
        {
            for (size_t i = 0; i <10; i++)
            if(log_data.ticket[i].id==0)
           {
            printf("inset2");
            if(1==num)
            S_list_add_id(Flight_sys.GOLD_USER,S_List_find_name(Flight_sys.GOLD_USER,log_data.name),num_id,i,flight_fare*0.8);
            Sys_ticket_num(Flight_sys,num_id ,-1);
            S_List_File_print(Flight_sys.GOLD_USER,GOLD_FILE); 
             return 0;
           }
        }
        else if(2==num)
        {
            S_list_add_id(Flight_sys.GOLD_USER,S_List_find_name(Flight_sys.GOLD_USER,log_data.name),0,num_d,(-flight_fare)*0.8);
            Sys_ticket_num(Flight_sys,num_id ,1);
            S_List_File_print(Flight_sys.GOLD_USER,GOLD_FILE); 
            return 0;
        } 
         
    }
     if(log_data.grade==5)
    {    printf("inset1");
        if(1==num) {
        for (size_t i = 0; i <10; i++) 
            if(log_data.ticket[i].id==0)
           {
            printf("inset2");
           
            S_list_add_id(Flight_sys.PLATINUM_USER,S_List_find_name(Flight_sys.PLATINUM_USER,log_data.name),num_id,i,flight_fare*0.7);
            Sys_ticket_num(Flight_sys,num_id ,-1);
            S_List_File_print(Flight_sys.PLATINUM_USER,PLATINUM_FILE); 
            return 0;
           }
        }
         else if(2==num)
        {
            S_list_add_id(Flight_sys.PLATINUM_USER,S_List_find_name(Flight_sys.PLATINUM_USER,log_data.name),0,num_d,(-flight_fare)*0.7);
            Sys_ticket_num(Flight_sys,num_id ,1);
            S_List_File_print(Flight_sys.PLATINUM_USER,PLATINUM_FILE); 
             return 0;
        } 

        
    }
    
    

    return EOF;
}

U_data Sys_user_log_updata(U_list *list,char *name)
{

if (list == NULL)
    {
        printf("S_List_log_in : list = NULL\n");
        exit(-1);
    }
    if (list->next == NULL)
    {
        printf("S_List_log_in : list->next = NULL\n");
        exit(-1);
    }
    while (1)
    {
        U_node *ptr2 = list->next;  

        while (ptr2 != NULL)
        {   
            // 比较用户名
            if (strcmp(name, ptr2->data.name) == 0)
            {       
                
                return ptr2->data; 
            }
            ptr2 = ptr2->next;
        }
       
    }
}


//用户自动升级
int Sys_user_up(FlightSystem Flight_sys,U_data log_data)
{   
// 普通用户信息文件                        GRADE_REGULAR 
// 银卡会员信息文件(9折) 余额 10000         GRADE_SILVER  
// 金卡会员信息文件(8折) 余额 15000         GRADE_GOLD    
// 白金会员信息文件(7折) 余额 20000         GRADE_PLATINUM
    U_data log_data2;
    if(log_data.grade==2)
    log_data2=Sys_user_log_updata(Flight_sys.REGULAR_USER,log_data.name);

    if(log_data.grade==3)
    log_data2=Sys_user_log_updata(Flight_sys.SILVER_USER,log_data.name);

    if(log_data.grade==4)
    log_data2=Sys_user_log_updata(Flight_sys.GOLD_USER,log_data.name);

    if(log_data.grade==5)
    log_data2=Sys_user_log_updata(Flight_sys.PLATINUM_USER,log_data.name);


    printf("grade:%d\r\n",log_data.grade);
    if(log_data2.grade!=1&&log_data2.value>=10000&&2==log_data2.grade)
    {   
        U_data data= S_List_find_name_data(Flight_sys.REGULAR_USER, log_data.name,GRADE_SILVER);
      
        S_List_name_delete(Flight_sys.REGULAR_USER,log_data.name);
        S_List_File_print(Flight_sys.REGULAR_USER,REGULAR_FILE); 

       
        printf("恭喜升级\n用户 %s :\n余额：%d\nREGULAR_USER ->>SILVER_USER\n",log_data.name,log_data2.value);
        S_List_insert(Flight_sys.SILVER_USER,data,1);
        S_List_File_print(Flight_sys.SILVER_USER,SILVER_FILE);
        log_data.grade=3;
    }
    if(log_data.grade!=1&&log_data.value>=15000&&3==log_data.grade)
    {   
        U_data data=S_List_find_name_data(Flight_sys.SILVER_USER, log_data.name,GRADE_GOLD);

        S_List_name_delete(Flight_sys.SILVER_USER,log_data.name);
        S_List_File_print(Flight_sys.SILVER_USER,SILVER_FILE); 

        printf("恭喜升级\n用户 %s :\n余额：%d\nSILVER_USER ->>GOLD_USER",log_data.name,log_data2.value);

        S_List_insert(Flight_sys.GOLD_USER,data,1);
        S_List_File_print(Flight_sys.GOLD_USER,GOLD_FILE);
        log_data.grade=4;
    }
    if(log_data.grade!=1&&log_data.value>=20000&&4==log_data.grade)
    {   U_data data=S_List_find_name_data(Flight_sys.GOLD_USER, log_data.name,GRADE_PLATINUM);
        S_List_name_delete(Flight_sys.GOLD_USER,log_data.name);
        S_List_File_print(Flight_sys.GOLD_USER,GOLD_FILE); 

        printf("恭喜升级\n用户 %s :\n余额：%d\nGOLD_USER ->>PLATINUM_USER",log_data.name,log_data2.value);
      
        S_List_insert(Flight_sys.PLATINUM_USER,data,1);
        S_List_File_print(Flight_sys.PLATINUM_USER,PLATINUM_FILE);
        log_data.grade=5;
    }

    return 0;
}

//充值金额

int Sys_user_recharge(FlightSystem Flight_sys,U_data log_data)
{
    int num;
    system("clear");
    printf("请输入充值金额：");

    scanf("%d",&num);


        if(log_data.grade==2)
    {   
        S_list_add_recharge(Flight_sys.REGULAR_USER,S_List_find_name(Flight_sys.REGULAR_USER,log_data.name),num);
        S_List_File_print(Flight_sys.REGULAR_USER,REGULAR_FILE); 
        
        printf("用户：%s \n当前余额 ：%d\n",log_data.name,log_data.value+num);
        
        return 0;  
    }
    
     if(log_data.grade==3)
     {
        S_list_add_recharge(Flight_sys.SILVER_USER,S_List_find_name(Flight_sys.SILVER_USER,log_data.name),num);
   
        S_List_File_print(Flight_sys.SILVER_USER,SILVER_FILE);  

        printf("用户：%s \n当前余额 ：%d\n",log_data.name,log_data.value+num);
        return 0;
     }

    if(log_data.grade==4)
    {
        S_list_add_recharge(Flight_sys.GOLD_USER,S_List_find_name(Flight_sys.GOLD_USER,log_data.name),num);
   
        S_List_File_print(Flight_sys.GOLD_USER,GOLD_FILE);  

        printf("用户：%s \n当前余额 ：%d\n",log_data.name,log_data.value+num);
        return 0;
    }
    
    if(log_data.grade==5)
    {
        S_list_add_recharge(Flight_sys.PLATINUM_USER,S_List_find_name(Flight_sys.PLATINUM_USER,log_data.name),num);
   
        S_List_File_print(Flight_sys.PLATINUM_USER,PLATINUM_FILE);  

        printf("用户：%s \n当前余额 ：%d\n",log_data.name,log_data.value+num);
        return 0;
    }
    




    return EOF;


    return 0;
}


 /*
    #define ADMIN_FILE "data/admin_users.txt"           // 管理员账户信息文件
    #define REGULAR_FILE "data/regular_users.txt"       // 普通用户信息文件
    #define SILVER_FILE "data/silver_members.txt"       // 银卡会员信息文件(9折) 余额 10000
    #define GOLD_FILE "data/gold_members.txt"           // 金卡会员信息文件(8折) 余额 15000
    #define PLATINUM_FILE "data/platinum_members.txt"   // 白金会员信息文件(7折) 余额 20000
    list = S_List_merge(list, Flight_sys.ADMIN_USER);    // 管理员账户信息文件
    list = S_List_merge(list, Flight_sys.REGULAR_USER);  // 普通用户信息文件
    list = S_List_merge(list, Flight_sys.SILVER_USER);   // 银卡会员信息文件 (9折)
    list = S_List_merge(list, Flight_sys.GOLD_USER);     // 金卡会员信息文件 (8折)
    list = S_List_merge(list, Flight_sys.PLATINUM_USER); // 白金会员信息文件 (7折)
*/
// 重置密码
int Sys_user_reset_password(FlightSystem Flight_sys,U_data log_data)
{   
    char password[32];
    printf("请输入新密码:");
    scanf("%s",password);

    if(log_data.grade==2)
    {   
        S_list_up_data_passwd(Flight_sys.REGULAR_USER,S_List_find_name(Flight_sys.REGULAR_USER,log_data.name),password);
        S_List_File_print(Flight_sys.REGULAR_USER,REGULAR_FILE); 
        return 0;  
    }
    
     if(log_data.grade==3)
     {
        S_list_up_data_passwd(Flight_sys.SILVER_USER,S_List_find_name(Flight_sys.SILVER_USER,log_data.name),password);
   
        S_List_File_print(Flight_sys.SILVER_USER,SILVER_FILE);  
        return 0;
     }

    if(log_data.grade==4)
    {
        S_list_up_data_passwd(Flight_sys.GOLD_USER,S_List_find_name(Flight_sys.GOLD_USER,log_data.name),password);
   
        S_List_File_print(Flight_sys.GOLD_USER,GOLD_FILE);  
        return 0;
    }
    
    if(log_data.grade==5)
    {
        S_list_up_data_passwd(Flight_sys.PLATINUM_USER,S_List_find_name(Flight_sys.PLATINUM_USER,log_data.name),password);
   
        S_List_File_print(Flight_sys.PLATINUM_USER,PLATINUM_FILE);  
        return 0;
    }
    
    return EOF;
}

//查看航班
void Sys_user_view_flight(FlightSystem Flight_sys)
{
    D_List_print( Flight_sys.Flight_list);
    printf("\n\n回车以继续\n\n");
    getchar();
    getchar();
    

}

void Sys_user_view_orders(FlightSystem Flight_sys,U_data log_data)
{   
    int flag=0;
    printf("\n\n用户: %s\n",log_data.name);
    for (size_t i = 0; i < 10; i++)
    {
        if(log_data.ticket[i].id!=0)
        {
        F_node *ptr= D_List_find_id(Flight_sys.Flight_list,log_data.ticket[i].id);
        printf("班次    机型        起点站  终点站  起飞时间    到达时间    飞行时间    票价  载客量  剩余座位  是否飞\n");
        printf("--------------------------------------------------------------------------------------------------\n");
        printf("%-6d", ptr->data.id);
        printf("%-15s", ptr->data.flighttype);
        printf("%-10s", ptr->data.startcity);
        printf("%-10s", ptr->data.arrivecity);
        printf("%02d:%02d\t", ptr->data.starttime[0], ptr->data.starttime[1]);
        printf("%02d:%02d\t    ", ptr->data.arrivetime[0], ptr->data.arrivetime[1]);
        printf("%-10s\t", ptr->data.flytime);
        printf("%-6d", ptr->data.value);
        printf("%-8d", ptr->data.maxNUM);
        printf("%-8d  ", ptr->data.leftbuyersa);
        printf("%c\n", ptr->data.whetherfly);
        flag=1;
        }
    }
    if (flag==0)
    {
        printf("\n没有机票,订单为空\n");
    }
    printf("\n\n__________回车以继续_________\n");
    getchar();
    getchar();
    
   
  

}


//


